package com.diqye.templet.util;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.diqye.templet.core.ReplaceStr;
import com.diqye.templet.method.ArrayBraceMethod;
import com.diqye.templet.method.ArrayBraceMethodForSql;
import com.diqye.templet.method.MapBraceMethod;
import com.diqye.templet.method.MapBraceMethodForSql;
import com.diqye.templet.regullation.BorderRegullation;
/**
 * 字符串操作辅助类
 *
 * @author 秦振龙
 * @date 2013-7-8
 *
 * <b>修改记录及说明：</b>
 * <ul>
 * <li>2013-7-8  秦振龙  创建</li>
 * </ul>
 */
public class Strings {

	private static ReplaceStr rsformart = new ReplaceStr(new BorderRegullation("{","}"));
	private static ReplaceStr formartforsql = new ReplaceStr(new BorderRegullation("#{","}"));
	private static ReplaceStr formartfill = new ReplaceStr(new BorderRegullation("${","}"));
	private static ReplaceStr $formart = new ReplaceStr(new BorderRegullation("$","$"));
	/**
	 * {0},{1} or {key1},{key2}
	 * @param str
	 * @param oMap
	 * @return
	 */
	public static String format(String str,Object...objects){
		return rsformart.replace(str, new ArrayBraceMethod(objects));
	}
	
	@SuppressWarnings("rawtypes")
	public static String format(String str,Map oMap){
		return rsformart.replace(str, new MapBraceMethod(oMap));
	}
	/**
	 * $0$,$1$ or $key1$,$key2$
	 * @param str
	 * @param oMap
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static String format$(String str,Map oMap){
		return $formart.replace(str, new MapBraceMethod(oMap));
	}
	
	public static String format$(String str,Object...objects){
		return $formart.replace(str, new ArrayBraceMethod(objects));
	}
	/**
	 * 功能类似format
	 * @param str
	 * @param objects
	 * @return
	 */
	public static String formatfill(String str,Object...objects){
		return formartfill.replace(str,new ArrayBraceMethod(objects,2));
	}
	@SuppressWarnings("rawtypes")
	public static String formatfill(String str,Map oMap){
		return formartfill.replace(str,new MapBraceMethod(oMap,2));
	}
	
	/**
	 * 将key转换为?值放入一个list中对应里面的？
	 * @param str
	 * @param objects
	 * @return
	 */
	public static Sqlpara formatsql(String str,Object...objects){
		ArrayBraceMethodForSql method = new ArrayBraceMethodForSql(objects,2);
		String sql = formartforsql.replace(str,method);
		Sqlpara sqlpara = new Sqlpara();
		sqlpara.sql = sql;
		sqlpara.paras = method.getParams();
		return sqlpara;
	}
	@SuppressWarnings("rawtypes")
	public static Sqlpara formatsql(String str,Map oMap){
		MapBraceMethodForSql method = new MapBraceMethodForSql(oMap,2);
		String sql = formartforsql.replace(str,method);
		Sqlpara sqlpara = new Sqlpara();
		sqlpara.sql = sql;
		sqlpara.paras = method.getParams();
		return sqlpara;
	}
	
	@SuppressWarnings("rawtypes")
	public static String join(Iterator iter,String sep){
		StringBuffer sbuf = new StringBuffer();
		while(iter.hasNext()){
			sbuf.append(iter.next());
			if(iter.hasNext()){
				sbuf.append(sep);
			}
		}
		return sbuf.toString();
	}
	public static String join(Object[] os,String sep){
		StringBuffer sbuf = new StringBuffer();
		for (int i = 0; i < os.length; i++) {
			sbuf.append(os[i]);
			if(i < os.length-1){
				sbuf.append(sep);
			}
		}
		return sbuf.toString();
	}
	@SuppressWarnings("rawtypes")
	public static String join(java.util.Collection collection,String sep){
		if(collection == null){
			return null;
		}
		return join(collection.iterator(),sep);
	}
	
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static List bachFormatfill(String formatstr,List list){
		List rlist = new ArrayList();
		rlist.addAll(list);
		for(int i=0;i<rlist.size();i++){
			Object o = rlist.get(i);
			rlist.remove(i);
			rlist.add(i, formatfill(formatstr,o));
		}
		return rlist;
	}
}
